package controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import entity.Comment;
import entity.HomeNews;
import entity.Note;
import entity.NoteResult;
import entity.NoteType;
import entity.School;
import entity.User;
import net.sf.json.JSONObject;
import service.CommentService;
import service.HomeNewsService;
import service.NoteService;
import service.NoteTypeService;
import service.SchoolService;
import service.UserService;
import util.ResponseUtil;
import util.ResultFormatUtil;

@Controller
@RequestMapping("/note")
public class NoteController {
	@Resource
	private NoteService noteService;
	@Resource
	private NoteTypeService noteTypeService;
	@Resource
	private SchoolService schoolService;
	@Resource
	private UserService userService;

	// http://localhost:8080/CampusApp/note/getNoteListData.do?typeId=1
	@RequestMapping("/getNoteListData")
	public void getNoteListData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type_Id = request.getParameter("typeId");
		int typeId = Integer.parseInt(type_Id);
		if (typeId > 0) {
			List<Note> noteListByTypeId = noteService.getNoteListByTypeId(typeId);
			if (noteListByTypeId != null) {
				// User
				List<NoteResult> noteResults = new ArrayList<>();
				for (int i = 0; i < noteListByTypeId.size(); i++) {
					Note note = noteListByTypeId.get(i);
					int editorId = note.getEditorId();
					int typeId2 = note.getTypeId();
					User editor = userService.getUserByUserId(editorId);
					//
					NoteResult result = new NoteResult();
					result.setNote(note);
					result.setEditorName(editor.getUserName());
					NoteType noteType = noteTypeService.getNoteTypeByTypeId(note.getTypeId());
					result.setNoteType(noteType);
					School school = schoolService.getSchoolBySchoolId(editor.getSchooleId());
					result.setSchool(school);
					noteResults.add(result);
				}
				// NoteType
				JSONObject result = ResultFormatUtil.getResultFrom(true, "noteList获取成功", noteResults);
				ResponseUtil.write(response, result);
			} else {
				JSONObject result = ResultFormatUtil.getResultFrom(false, "noteList获取失败", null);
				ResponseUtil.write(response, result);
			}
		} else {
			JSONObject result = ResultFormatUtil.getResultFrom(false, "typeId转型失败", null);
			ResponseUtil.write(response, result);
		}

	}
	// http://localhost:8080/CampusApp/note/deleteNoteByNoteId.do?noteId=1
	@RequestMapping("/deleteNoteByNoteId")
	public void deleteNoteByNoteId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String note_Id = request.getParameter("noteId");
		int noteId = Integer.parseInt(note_Id);
		if (noteId > 0) {
			int count=noteService.deleteNoteByNoteId(noteId);
			if(count>0){
				JSONObject result = ResultFormatUtil.getResultFrom(true, "note删除成功", null);
				ResponseUtil.write(response, result);
			}else{
				JSONObject result = ResultFormatUtil.getResultFrom(false, "note删除失败", null);
				ResponseUtil.write(response, result);
			}
		}else{
			JSONObject result = ResultFormatUtil.getResultFrom(false, "note删除失败", null);
			ResponseUtil.write(response, result);
		}
	}
	// http://localhost:8080/CampusApp/note/getMyNoteListData.do?userId=1
	@RequestMapping("/getMyNoteListData")
	public void getMyNoteListData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String user_Id = request.getParameter("userId");
		int userId = Integer.parseInt(user_Id);
		if (userId > 0) {
			List<Note> noteListByUserId = noteService.getNoteListByUserId(userId);
			if (noteListByUserId != null) {
				List<NoteResult> noteResults = new ArrayList<>();
				for (int i = 0; i < noteListByUserId.size(); i++) {
					Note note = noteListByUserId.get(i);
					int editorId = note.getEditorId();
					int typeId2 = note.getTypeId();
					User editor = userService.getUserByUserId(editorId);
					//
					NoteResult result = new NoteResult();
					result.setNote(note);
					result.setEditorName(editor.getUserName());
					NoteType noteType = noteTypeService.getNoteTypeByTypeId(note.getTypeId());
					result.setNoteType(noteType);
					School school = schoolService.getSchoolBySchoolId(editor.getSchooleId());
					result.setSchool(school);
					noteResults.add(result);
				}
				// NoteType
				JSONObject result = ResultFormatUtil.getResultFrom(true, "noteList获取成功", noteResults);
				ResponseUtil.write(response, result);
			}else {
				JSONObject result = ResultFormatUtil.getResultFrom(false, "noteList获取失败", null);
				ResponseUtil.write(response, result);
			}
		}
	}

	// http://localhost:8080/CampusApp/note/publishNote.do?typeId=1&content=newNote&editor=1
	@RequestMapping("/publishNote")
	public void publishNote(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		int typeId = 1;
		List<NoteType> noteTypes = noteTypeService.selectAllNoteType();
		for (int i = 0; i < noteTypes.size(); i++) {
			NoteType noteType = noteTypes.get(i);
			if ((noteType.getType()).equals(type)) {
				typeId = i + 1;
				break;
			}
		}
		String editor_Id = request.getParameter("editorId");
		int editorId = Integer.parseInt(editor_Id);
		String content = request.getParameter("content");
		String title = request.getParameter("title");
		String imgUrl = request.getParameter("imgUrl");
		Note newNote = new Note();
		// int typeId=1;
		// String content="这是new帖子";
		// int editorId=1;
		// String title="标题";
		// String imgUrl="";
		newNote.setTypeId(typeId);
		newNote.setContent(content);
		newNote.setEditorId(editorId);
		newNote.setTitle(title);
		newNote.setImgUrl(imgUrl);
		//
		int flag = noteService.addNewNote(newNote);
		if (flag > 0) {
			JSONObject result = ResultFormatUtil.getResultFrom(true, "新帖子发布成功", null);
			ResponseUtil.write(response, result);
		} else {
			JSONObject result = ResultFormatUtil.getResultFrom(false, "新帖子发布失败", null);
			ResponseUtil.write(response, result);
		}
	}

	// @Resource
	// private HomeNewsService homeNewsService;
	// //http://localhost:8080/CampusApp/homeNews/getHomeNewData.do
	// @RequestMapping("/getHomeNewData")
	// public void getHomeNewData(HttpServletRequest request,
	// HttpServletResponse response) throws Exception {
	// List<HomeNews> allHomeNews = homeNewsService.getAllHomeNews();
	// if (allHomeNews!=null) {
	// JSONObject result = ResultFormatUtil.getResultFrom(true,
	// "HomeNews集合获取成功", allHomeNews);
	// ResponseUtil.write(response, result);
	// }else{
	// JSONObject result = ResultFormatUtil.getResultFrom(false,
	// "HomeNews集合获取失败", allHomeNews);
	// ResponseUtil.write(response, result);
	// }
	//
	//
	// }

	// http://192.168.16.140:8080/CampusApp/user/adduser.do

}
